﻿using System;
using WickedFlame.Core.Shapes;
using WickedFlame.Core.State;

namespace WickedFlame.Core.History
{
    /// <summary>
    /// Add new vectorshape command
    /// </summary>
    public class AddCommand : HistoryCommand
    {
        VectorShape _shape;
        ShapeMemento memento;

        public AddCommand(VectorShape shape, int layerIndex)
            : base(layerIndex)
        {
            Title = "Shape added";

            // Keep reference of added object
            _shape = shape;
            if (shape is IStateCapturable)
                memento = ((IStateCapturable)_shape).CreateMementoState();
        }

        public override void Undo(ShapeList list)
        {
            list.Remove(_shape);
        }

        public override void Redo(ShapeList list)
        {
            list.UnselectAll();
            
            if (memento != null && _shape is IStateCapturable)
                ((IStateCapturable)_shape).ResetState(memento);

            list.Add(_shape);
        }
    }
}
